package com.goldgov.learnmanage.service.impl;

import java.text.DecimalFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.learnmanage.dao.CourseDao;
import com.goldgov.learnmanage.dao.UserDao;
import com.goldgov.learnmanage.service.Course;
import com.goldgov.learnmanage.service.CourseQuery;
import com.goldgov.learnmanage.service.CourseService;
import com.goldgov.learnmanage.service.User;
import com.goldgov.learnmanage.service.UserQuery;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
@Service
public class CourseServiceImpl implements CourseService {

	@Autowired
	CourseDao courseDao;
	@Autowired
	UserDao userDao;
	
	@Override
	public List<Course> getCourseList(CourseQuery query) {
		return courseDao.getCourseList(query);
	}
	@Override
	public List<Course> getCourseLearnList(CourseQuery query) {
		return courseDao.getCourseLearnList(query);
	}
	@Override
	public List<User> findPersonLearnTime(UserQuery query) {
		//查询支部里的所有人
		List<User> userList=userDao.findOrgUser(query.getSearchOrgID());
		if(userList!=null&&userList.size()>0) {
			List<Course> list=courseDao.findPersonLearnTime(query);
			for(User userBean:userList) {
				for(Course bean:list) {
					if(userBean.getUserID().equals(bean.getUserID())) {
						userBean.setCurrentCourseTime(bean.getCurrentCourseTime());
						break;
					}
				}
			}
		}
		//从大到小排序
		List<User> list2=userList.stream()
				.sorted(Comparator.comparing(User::getCurrentCourseTime).reversed())
				.collect(Collectors.toList());
		return list2;
	}
	@Override
	public List<Course> findOrgAvgLearnTime(CourseQuery query) {
		//根据时长从大到小排序
		List<Course> list=courseDao.findOrgAvgLearnTime(query);
		DecimalFormat df = new DecimalFormat("0.00");//格式化小数 
		//计算平均数
		for(Course c:list) {
			if(PropertyUtil.objectNotEmpty(c.getCurrentCourseTime())) {
				c.setAvgCourseTime(Double.valueOf(df.format((double)c.getCurrentCourseTime()/c.getOrgNum())));
			}else {
				c.setAvgCourseTime(0);
			}
		}
		List<Course> list2= list.stream()
				.sorted(Comparator.comparing(Course::getAvgCourseTime).reversed())
				.collect(Collectors.toList());
		return list2;
	}
	@Override
	public List<Course> getCourseLearnTimeList(CourseQuery query) {
		List<Course> list=courseDao.getCourseLearnTimeList(query);
		//处理课程学习百分比
		DecimalFormat df = new DecimalFormat("0.00%");
		for(Course course:list) {
			if(PropertyUtil.objectNotEmpty(course.getCourseTime())) {
				if(course.getCourseTime()<=course.getCurrentCourseTime()) {
					course.setLearingPercentum("100%");
				}else {
					course.setLearingPercentum(df.format(course.getCurrentCourseTime()/(double)course.getCourseTime()));
				}
			}
		}
		return list;
	}

	/**
	 * 课程学习热度
	 * @param query
	 * @return
	 */
	public List<Course> findCourseLearnHead(CourseQuery query){
		if (!PropertyUtil.objectNotEmpty(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		return courseDao.findCourseLearnHead(query);
	}
	/**
	 * 组织学习完成时长对比
	 * @param query
	 * @return
	 */
	public List<Course> findOrgWCDContrast(CourseQuery query){
		if (!PropertyUtil.objectNotEmpty(query.getSearchYear())) {
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		return courseDao.findOrgWCDContrast(query);
	}
	/**
	 * 查询是否含有市工委县
	 * @date 2018年12月13日 下午3:44:32 
	 * @author hanxiangyu
	 * @param code
	 * @return
	 */
	public int findOrgSGWXNum(String orgId) {
		return courseDao.findOrgSGWXNum(orgId);
	}
	
	/**
	 * 人员
	 * @date 2018年12月19日 下午5:37:50 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<Course> findLDDYStudyCourseListByUserId(CourseQuery query){
		if (PropertyUtil.objectNotEmpty(query.getSearchType())) {
			switch (query.getSearchType()) {
			case 3:
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			case 4:
				query.setSearchParam(DateUtil.getSeason(new Date()));
				break;
			case 5:
				query.setSearchParam(DateUtil.getYear(new Date()));
				break;
			default:
				query.setSearchType(3);
				query.setSearchParam(DateUtil.getMonth(new Date()));
				break;
			}
		}else {
			query.setSearchParam(DateUtil.getMonth(new Date()));
		}
		return courseDao.findLDDYStudyCourseListByUserId(query);
	}
}
